package sdk.webview.fmc.com.fmcsdk.activity;

import android.content.pm.ActivityInfo;
import android.media.AudioManager;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Display;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import sdk.webview.fmc.com.fmcsdk.R;
import sdk.webview.fmc.com.fmcsdk.base.BaseActivity;
import sdk.webview.fmc.com.fmcsdk.base.BasePresenter;
import sdk.webview.fmc.com.fmcsdk.util.TitleBar;
import sdk.webview.fmc.com.fmcsdk.videoview.IjkVideoView;
import tv.danmaku.ijk.media.player.IMediaPlayer;
import tv.danmaku.ijk.media.player.IjkMediaPlayer;


public class RtmpVideoActivity extends BaseActivity implements View.OnClickListener {

    private AudioManager mAudioManager;
    private int mCurrentVolume;
    private int mMaxVolume = 10;
    private IjkVideoView mVideoView;
    private String mUrl="";
    private RelativeLayout mLoadingLayout;
    private TitleBar titlebar;
    private LinearLayout ll_bottom;
    private RelativeLayout rl_top;
    private ImageView iv_screen;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_rtmp_video;
    }

    @Override
    protected void init(Bundle savedInstanceState) {

        initAudio();
        initView();
        initVideo();
    }



    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    private void initAudio() {
        mAudioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
        setVolumeControlStream(AudioManager.STREAM_MUSIC);
        mAudioManager.requestAudioFocus(null, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
        mMaxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC) * 10;// 系统声音取值是0-10,*10为了和百分比相关
        mCurrentVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC) * 10;
    }

    /**
     * 初始化控件
     */
    private void initView() {
        String rtmpId = getIntent().getStringExtra("rtmpId");
        String rtmpName = getIntent().getStringExtra("rtmpName");
        String rtmpType = getIntent().getStringExtra("rtmpType");
        String rtmpAddress = getIntent().getStringExtra("rtmpAddress");
        String rtmpStatus = getIntent().getStringExtra("rtmpStatus");
        String rtmpUrl = getIntent().getStringExtra("rtmpUrl");
        mUrl = rtmpUrl;
        titlebar = (TitleBar) findViewById(R.id.titlebar);
        titlebar.setTitle("视频监控");
        TextView tv_video_name = (TextView) findViewById(R.id.tv_video_name);
        tv_video_name.setText(rtmpName);
        TextView tv_video_address = (TextView) findViewById(R.id.tv_video_address);
        tv_video_address.setText(rtmpAddress);
        TextView tv_video_type = (TextView) findViewById(R.id.tv_video_type);
        tv_video_type.setText(rtmpType);
        TextView tv_video_address2 = (TextView) findViewById(R.id.tv_video_address2);
        tv_video_address2.setText(rtmpAddress);
        TextView tv_video_status = (TextView) findViewById(R.id.tv_video_status);
        if (TextUtils.isEmpty(rtmpStatus)){
            tv_video_status.setText("离线");
        }else {
            tv_video_status.setText("在线");
        }

        mVideoView = (IjkVideoView) findViewById(R.id.video_view);
        iv_screen = (ImageView) findViewById(R.id.iv_screen);
        ll_bottom = (LinearLayout) findViewById(R.id.ll_bottom);
        rl_top = (RelativeLayout) findViewById(R.id.rl_top);
        iv_screen.setOnClickListener(this);
        ImageView iv_update = (ImageView) findViewById(R.id.iv_update);
        iv_update.setOnClickListener(this);
        mLoadingLayout = (RelativeLayout) findViewById(R.id.rl_loading_layout);

    }
    /**
     * 初始化播放器
     */
    private void initVideo() {
        IjkMediaPlayer.loadLibrariesOnce(null);
        IjkMediaPlayer.native_profileBegin("libijkplayer.so");
        mVideoView.setVideoURI(Uri.parse(mUrl));
        mVideoView.setOnPreparedListener(new IMediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(IMediaPlayer mp) {
                mVideoView.start();
            }
        });
        mVideoView.setOnInfoListener(new IMediaPlayer.OnInfoListener() {
            @Override
            public boolean onInfo(IMediaPlayer mp, int what, int extra) {
                switch (what) {
                    case IjkMediaPlayer.MEDIA_INFO_BUFFERING_START:
                        mLoadingLayout.setVisibility(View.VISIBLE);
                        break;
                    case IjkMediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START:
                    case IjkMediaPlayer.MEDIA_INFO_BUFFERING_END:
                        mLoadingLayout.setVisibility(View.GONE);
                        break;
                }
                return false;
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        //释放audiofocus
        mAudioManager.abandonAudioFocus(null);
    }
    @Override
    protected void onStop() {
        super.onStop();
        if (mVideoView != null) {
            mVideoView.stopPlayback();
        }
    }

    @Override
    public void onClick(View v) {

            if (v.getId()==R.id.iv_screen) {
                Display display = getWindowManager().getDefaultDisplay();
                int width = display.getWidth();
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                titlebar.setVisibility(View.GONE);
                ll_bottom.setVisibility(View.GONE);
                iv_screen.setVisibility(View.GONE);
                int statusBarHeight= -1;
                //获取status_bar_height资源的ID
                int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
                if (resourceId > 0) {
                    //根据资源ID获取响应的尺寸值
                    statusBarHeight = getResources().getDimensionPixelSize(resourceId);
                }

                mVideoView.getLayoutParams().height = width-statusBarHeight;
                rl_top.getLayoutParams().height = width-statusBarHeight;
            }else if (v.getId()==R.id.iv_update){
                mLoadingLayout.setVisibility(View.VISIBLE);
                mVideoView.setVideoURI(Uri.parse(mUrl));
            }

        }



    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode==KeyEvent.KEYCODE_BACK){
            if (titlebar.getVisibility()==View.GONE){
                titlebar.setVisibility(View.VISIBLE);
                ll_bottom.setVisibility(View.VISIBLE);
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                iv_screen.setVisibility(View.VISIBLE);
                mVideoView.getLayoutParams().height = dip2px(220);
                rl_top.getLayoutParams().height = dip2px(220);
            }else {
                finish();
            }

        }
        return true;
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public  int dip2px(float dpValue) {
        final float scale = getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    }
